# ~*~ coding: utf-8 ~*~
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms.validators import InputRequired

from apps.common.form import ErrorFormMinx
from apps.common.validators import Chinese, Phone, Email, Date
from apps.common.utils import date_str_to_timestamp


__all__ = ['UserCreateUpdateForm']


class UserCreateUpdateForm(ErrorFormMinx, FlaskForm):
    name = StringField(validators=[InputRequired('姓名不能为空'), Chinese('姓名必须是中文')])
    username = StringField(validators=[InputRequired('工号不能为空')])
    roles = StringField()
    email = StringField(validators=[InputRequired('邮箱不能为空'), Email('邮箱格式不正确')])
    phone = StringField(validators=[InputRequired('手机号码不能为空'), Phone('手机号码格式不正确')])
    is_admin = StringField()
    otp_level = StringField()

    class Meta:
        csrf = True

    @property
    def data(self):
        return {
            'name': self.name.data,
            'username': self.username.data,
            'roles': self.roles.data,
            'email': self.email.data,
            'phone': self.phone.data,
            'is_admin': int(self.is_admin.data) if self.is_admin.data else 0,
            'otp_level': int(self.otp_level.data) if self.otp_level.data else 2,
        }
